package cn.threewaters.qtyy.config.dyds;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;

import lombok.extern.slf4j.Slf4j;

//@Configuration
//@AutoConfigureAfter({ DataSourceConfig.class })
@Slf4j
public class MybatisConfiguration extends MybatisAutoConfiguration {

	public MybatisConfiguration(MybatisProperties properties, ObjectProvider<Interceptor[]> interceptorsProvider,
			ResourceLoader resourceLoader, ObjectProvider<DatabaseIdProvider> databaseIdProvider,
			ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider) {
		super(properties, interceptorsProvider, resourceLoader, databaseIdProvider, configurationCustomizersProvider);
	}

	@Bean
	@ConfigurationProperties(prefix = "mybatis-plus1")
	@ConfigurationPropertiesBinding()
	@Primary
	public MybatisSqlSessionFactoryBean sqlSessionFactorys1(@Qualifier(value = "datasource") DataSource dataSource) {
		log.info("-------------------- 重载父类 sqlSessionFactory1 init ---------------------");
		MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		return bean;
	}

	@Bean
	@ConfigurationProperties(prefix = "mybatis-plus2")
	@ConfigurationPropertiesBinding()
	public MybatisSqlSessionFactoryBean sqlSessionFactorys2(@Qualifier(value = "datasource") DataSource dataSource) {
		log.info("-------------------- 重载父类 sqlSessionFactory2 init ---------------------");
		MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		return bean;
	}

	/**
	 * 有多少个数据源就要配置多少个bean
	 * 
	 * @return
	 */
	@Bean(name = "datasource")
	public AbstractRoutingDataSource roundRobinDataSouceProxy(@Qualifier(value = "datasource1") DataSource dataSource1,
			@Qualifier(value = "datasource2") DataSource dataSource2) {
		DynamicDataSource bean = new DynamicDataSource();
		Map<Object, Object> targetDataSources = new HashMap<>();
		targetDataSources.put("datasource1", dataSource1);
		targetDataSources.put("datasource2", dataSource2);
		bean.setTargetDataSources(targetDataSources);
		bean.setDefaultTargetDataSource(dataSource1);
		return bean;
	}

}